package com.cloudxingkong.dianyinke.NewTeacher.Fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Adapter.MTWeishangkeAdapter;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.NewTeacher.bean.StudentState;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 云星空科技 on 2016/3/17.
 *
 * 我的学生
 *  ~ 已结款Fragment
 */
public class YiFuKuangFragment extends BaseFragment {
    private ListView listView;
    private SharedPreferences sharedPreferences;
    private String sessionID;
    private ProgressBar progressBar;

    private List<StudentState.ListBean> data;
    private MTWeishangkeAdapter adapter;
    private static final String TYPE_YFK="1";
    private TextView yfkMsg;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_yifukuan, null);
        yfkMsg= (TextView) view.findViewById(R.id.yfk_tv_msg);
        progressBar= (ProgressBar) view.findViewById(R.id.pb_yifukuan);
        listView= (ListView) view.findViewById(R.id.fragment_yifukuan_listview);
        sharedPreferences = getActivity().getSharedPreferences("data_dyk", 0);
        sessionID = CookieHelper.getSessionID(sharedPreferences);
        data=new ArrayList<>();
        adapter = new MTWeishangkeAdapter(data,getActivity(),TYPE_YFK);
        listView.setAdapter(adapter);


        loadYiFuKuan();


        return view;
    }

    private void loadYiFuKuan() {
        progressBar.setVisibility(View.VISIBLE);
        RequestParams params = new RequestParams(ThisUri.SERVER_YUMING + ThisUri.MY_STUDENTS);
        params.addHeader("cookie",sessionID);
        params.setMultipart(true);
        params.addBodyParameter("ispayed", "1");

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("test", "已付款 " + result);
                if (!result.equals("")&&result != null) {
                    Message msg=handler.obtainMessage();
                    msg.what=1;
                    msg.obj=result;
                    handler.sendMessage(msg);
                }else{
                    handler.sendEmptyMessage(2);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    String result= (String) msg.obj;
                    yfkMsg.setVisibility(View.GONE);
                    progressBar.setVisibility(View.GONE);
                    Gson gson=new Gson();
                    StudentState studentState = gson.fromJson(result, StudentState.class);
                    if (studentState.getList() != null && studentState.getList().size() > 0) {
                        data.addAll(studentState.getList());
                        adapter.notifyDataSetChanged();
                    }else{
                        Toast.makeText(getActivity(), "获取数据失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2:
                    progressBar.setVisibility(View.GONE);
                    yfkMsg.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };
}
